﻿// Copyright (c) ZhangPeihang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using PetaPoco;
using System.Reflection;

namespace LaserMark.DataAccess;

public class DataBaseMapper : ConventionMapper
{
    public override Func<object, object> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) =>
        targetProperty.PropertyType.IsEnum && sourceType == typeof(string)
            ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertFromDb
            : base.GetFromDbConverter(targetProperty, sourceType);

    public override Func<object, object> GetToDbConverter(PropertyInfo sourceProperty) =>
        sourceProperty.PropertyType.IsEnum
            ? new NumberToEnumConverter(sourceProperty.PropertyType).ConvertToDb
            : base.GetToDbConverter(sourceProperty);
}
